﻿using Mes.Pendant.MaterialTypeService.Dto;
using Mes.Pendant.MaterialTypeService.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mes.Pendant.Web.Controllers
{
	public class MaterialTypeController : Controller
	{
		private readonly IMaterialTypeServices _materialTypeServices;

		public MaterialTypeController(IMaterialTypeServices materialTypeServices)
		{
			_materialTypeServices = materialTypeServices;
		}

		public IActionResult Index()
		{
			return View();
		}

		public IActionResult Add(int orderBy)
		{
			ViewBag.orderBy = orderBy;
			return View();
		}

		[HttpGet]
		public async Task<JsonResult> GetMaterialTypeList(int page, int limit, string? materialTypeName)
		{
			var result = await _materialTypeServices.GetMaterialTypeList(page, limit, materialTypeName);
			return Json(new { data = result.Queryable, count = result.RowCount, code = 0, msg = "" });
		}

		public async Task<List<MaterialTypeListDto>> GetMaterialTypeTreeTable(string? materialTypeName)
		{
			var result =await _materialTypeServices.GetMaterialTypeTreeTable(  materialTypeName);
			return result;
		}


		[HttpGet]
		public ActionResult SelectPublicCode(string? materialTypeName)
		{
			try
			{
				var nodeList = GetMaterialTypeTreeTable(materialTypeName);
				return Json(new { data = nodeList.Result, count = nodeList.Result.Count });
			}
			catch (Exception)
			{
				return Json("");
			}
		}

		[HttpPost]
		public async Task<int> AddMaterialType(AddMaterialTypeDto addMaterialTypeDto)
		{
			int result = await _materialTypeServices.AddMaterialType(addMaterialTypeDto);
			return result;
		}

	}
}
